


*set filepath. NOTE: you will need to update the filepath to run this file
cd "/Users/jeremyhorowitz/Documents/Book/Replication files/Chapter 4"



**************************************************************
********** Tables 4.2 & 4.3 **********************************
**************************************************************

*2007 election 
use "rally_data2007.dta", clear

nbreg kibaki_rallies swing_share voters area density distance_to_starehe major_town_count starehe, robust
margins, at(swing_share=(0 1) (mean) voters area density distance_to_starehe (median) major_town_count starehe)  
margins, dydx(swing_share) at((mean) _all (median) major_town_count starehe) post level(90)

nbreg odinga_rallies swing_share voters area density distance_to_starehe major_town_count starehe, robust
margins, at(swing_share=(0 1) (mean) voters area density distance_to_starehe (median) major_town_count starehe)  
margins, dydx(swing_share) at((mean) _all (median) major_town_count starehe) post level(90)

nbreg musyoka_rallies swing_share voters area density distance_to_starehe major_town_count starehe, robust
margins, at(swing_share=(0 1) (mean) voters area density distance_to_starehe (median) major_town_count starehe)  
margins, dydx(swing_share) at((mean) _all (median) major_town_count starehe) post level(90)


*2013 election
use "rally_data2013.dta", clear

nbreg kenyatta_rallies swing_share voters area major_town_count westlands distance density, robust
margins, at(swing_share=(0 1) (mean) voters area density distance (median) major_town_count westlands)  
margins, dydx(swing_share) at((mean) _all (median) major_town_count westlands) post level(90)

nbreg odinga_rallies swing_share voters area major_town_count westlands distance density, robust
margins, at(swing_share=(0 1) (mean) voters area density distance (median) major_town_count westlands)  
margins, dydx(swing_share) at((mean) _all (median) major_town_count westlands) post level(90)


*2017 election
use "rally_data2017.dta", clear

nbreg kenyatta_rallies swing_share17 voters area major_town_count westlands distance density, robust
margins, at(swing_share=(0 1) (mean) voters area density distance (median) major_town_count westlands)  
margins, dydx(swing_share) at((mean) _all (median) major_town_count westlands) post level(90)

nbreg odinga_rallies swing_share17 voters area major_town_count westlands distance density, robust
margins, at(swing_share=(0 1) (mean) voters area density distance (median) major_town_count westlands)  
margins, dydx(swing_share) at((mean) _all (median) major_town_count westlands) post level(90)






**************************************************************
********** Table 4.4 ****************************************
**************************************************************


*2007 election 
use "rally_data2007.dta", clear

gen luo_kam_share = luo_share + kamba_share
nbreg kibaki_rallies kikuyu_share luo_kam_share voters area density distance major_town_count starehe, robust

gen kik_kam_share=kikuyu_share + kamba_share
nbreg odinga_rallies luo_share kik_kam_share voters area density distance_to_starehe major_town_count starehe, robust

gen kik_luo_share=kikuyu_share + luo_share
nbreg musyoka_rallies kamba_share kik_luo_share voters area density distance major_town_count starehe, robust 


*2013 election
use "rally_data2013.dta", clear

nbreg kenyatta_rallies kikuyu_share luo_share voters area major_town_count westlands distance density, robust
nbreg odinga_rallies  luo_share kikuyu_share voters area major_town_count westlands distance density, robust


*2017 election
use "rally_data2017.dta", clear

nbreg kenyatta_rallies kikuyu_share luo_share voters area major_town_count westlands distance density, robust
nbreg odinga_rallies luo_share kikuyu_share voters area major_town_count westlands distance density, robust



**************************************************************
********** Table 4.5 *****************************************
**************************************************************

*2007 election 
use "rally_data2007.dta", clear

egen pnu_groups_share = rowtotal(kikuyu_share meru_share embu_share)
egen odm_groups_share = rowtotal(luo_share kalenjin_share luhya_share kisii_share)

nbreg kibaki_rallies pnu_groups_share luo_share kamba_share voters area density distance_to_starehe major_town_count starehe, robust 
nbreg odinga_rallies odm_groups_share kikuyu_share kamba_share voters area density distance_to_starehe major_town_count starehe, robust


*2013 election
use "rally_data2013.dta", clear

egen jub_groups_share = rowtotal(kikuyu_share kalenjin_share meru_share embu_share)
egen cord_groups_share = rowtotal(luo_share kamba_share mijikenda_share kisii_share luhya_share)

nbreg kenyatta_rallies jub_groups_share luo_share voters area major_town_count westlands distance density, robust
nbreg odinga_rallies cord_groups_share kikuyu_share voters area major_town_count westlands distance density, robust


*2017 election
use "rally_data2017.dta", clear

egen pnu_groups_share = rowtotal(kikuyu_share kalenjin_share meru_share embu_share)
egen nasa_groups_share = rowtotal(luo_share kamba_share mijikenda_share kisii_share luhya_share)

nbreg kenyatta_rallies pnu_groups_share luo_share voters area major_town_count westlands distance density, robust
nbreg odinga_rallies nasa_groups_share kikuyu_share voters area major_town_count westlands distance density, robust






**************************************************************
********** Figure 4.2 ****************************************
**************************************************************

***2007 election 
use "rally_data2007.dta", clear

gen kikuyu_core=0
replace kikuyu_core=1 if kikuyu_share>=.5
gen luo_core=0
replace luo_core=1 if luo_share>=.5
gen kamba_core=0
replace kamba_core=1 if kamba_share>=.5
gen luhya_core=0
replace luhya_core=1 if luhya_share>=.5
gen kalenjin_core=0
replace kalenjin_core=1 if kalenjin_share>=.5
gen kisii_core=0
replace kisii_core=1 if kisii_share>=.5
gen meru_core=0
replace meru_core=1 if meru_share>=.5
gen mijikenda_core=0
replace mijikenda_core=1 if mijikenda_share>=.5
gen maasai_core=0
replace maasai_core=1 if maasai_share>=.5
gen turkana_core=0
replace turkana_core=1 if turkana_share>=.5
gen embu_core=0
replace embu_core=1 if embu_share>=.5
gen taita_core=0
replace taita_core=1 if taita_share>=.5
gen teso_core=0
replace teso_core=1 if teso_share>=.5

*voter shares by ethnic areas
egen luhya_core_voters=total(voters) if luhya_core==1
egen kalenjin_core_voters=total(voters) if kalenjin_core==1
egen kisii_core_voters=total(voters) if kisii_core==1
egen meru_core_voters=total(voters) if meru_core==1
egen mijikenda_core_voters=total(voters) if mijikenda_core==1
egen maasai_core_voters=total(voters) if maasai_core==1
egen turkana_core_voters=total(voters) if turkana_core==1
egen embu_core_voters=total(voters) if embu_core==1
egen taita_core_voters=total(voters) if taita_core==1
egen teso_core_voters=total(voters) if teso_core==1
egen total_voters=total(voters)

gen luhya_core_share= luhya_core_voters/total_voters
gen kalenjin_core_share= kalenjin_core_voters/total_voters
gen kisii_core_share= kisii_core_voters/total_voters
gen meru_core_share= meru_core_voters/total_voters
gen mijikenda_core_share= mijikenda_core_voters/total_voters
gen maasai_core_share= maasai_core_voters/total_voters
gen turkana_core_share= turkana_core_voters/total_voters
gen embu_core_share= embu_core_voters/total_voters
gen taita_core_share= taita_core_voters/total_voters
gen teso_core_share= teso_core_voters/total_voters

tab luhya_core_share 
tab kalenjin_core_share 
tab kisii_core_share 
tab meru_core_share 
tab mijikenda_core_share 
tab maasai_core_share 
tab turkana_core_share 
tab embu_core_share 
tab taita_core_share 
tab teso_core_share

gen group="" in 1/13
replace group="1 Luhya Area" if _n==1
replace group="2 Kalenjin Area" if _n==2
replace group="3 Kisii Area" if _n==3
replace group="4 Meru Area" if _n==4
replace group="5 Mijikenda Area" if _n==5
replace group="6 Maasai Area" if _n==6
replace group="7 Turkana Area" if _n==7
replace group="8 Embu Area" if _n==8
replace group="9 Taita Area" if _n==9
replace group="10 Teso Area" if _n==10
replace group="11 Kikuyu Area" if _n==11
replace group="12 Luo Area" if _n==12
replace group="13 Kamba Area" if _n==13

gen num=_n in 1/13
gen kibshare=. in 1/13
gen odishare=. in 1/13
gen musshare=. in 1/13

*kibaki
egen krl=total(kibaki_rallies) if luhya_core==1
egen krk=total(kibaki_rallies) if kalenjin_core==1
egen krki=total(kibaki_rallies) if kisii_core==1
egen krme=total(kibaki_rallies) if meru_core==1
egen krmi=total(kibaki_rallies) if mijikenda_core==1
egen krmas=total(kibaki_rallies) if maasai_core==1
egen krturk=total(kibaki_rallies) if turkana_core==1
egen kremb=total(kibaki_rallies) if embu_core==1
egen krtai=total(kibaki_rallies) if taita_core==1
egen krteso=total(kibaki_rallies) if teso_core==1
egen krkik=total(kibaki_rallies) if kikuyu_core==1
egen krluo=total(kibaki_rallies) if luo_core==1
egen krkam=total(kibaki_rallies) if kamba_core==1

sort krl
replace krl=krl[_n-1] if krl==.
sort krk
replace krk=krk[_n-1] if krk==.
sort krki
replace krki=krki[_n-1] if krki==.
sort krme
replace krme= krme[_n-1] if krme==.
sort krmi
replace krmi= krmi[_n-1] if krmi==.
sort krmas
replace krmas= krmas[_n-1] if krmas==.
sort krturk
replace krturk= krturk[_n-1] if krturk==.
sort kremb
replace kremb= kremb[_n-1] if kremb==.
sort krtai
replace krtai= krtai[_n-1] if krtai==.
sort krteso
replace krteso= krteso[_n-1] if krteso==.
sort krkik
replace krkik= krkik[_n-1] if krkik==.
sort krluo
replace krluo= krluo[_n-1] if krluo==.
sort krkam
replace krkam= krkam[_n-1] if krkam==.

sort num
replace kibshare=krl/121 if _n==1
replace kibshare=krk/121 if _n==2
replace kibshare=krki/121 if _n==3
replace kibshare=krme/121 if _n==4
replace kibshare=krmi/121 if _n==5
replace kibshare=krmas/121 if _n==6
replace kibshare=krturk/121 if _n==7
replace kibshare=kremb/121 if _n==8
replace kibshare=krtai/121 if _n==9
replace kibshare=krteso/121 if _n==10
replace kibshare=krkik/121 if _n==11
replace kibshare=krluo/121 if _n==12
replace kibshare=krkam/121 if _n==13

*odinga
egen orl=total(odinga_rallies) if luhya_core==1
egen ork=total(odinga_rallies) if kalenjin_core==1
egen orki=total(odinga_rallies) if kisii_core==1
egen orme=total(odinga_rallies) if meru_core==1
egen ormi=total(odinga_rallies) if mijikenda_core==1
egen ormas=total(odinga_rallies) if maasai_core==1
egen orturk=total(odinga_rallies) if turkana_core==1
egen oremb=total(odinga_rallies) if embu_core==1
egen ortai=total(odinga_rallies) if taita_core==1
egen orteso=total(odinga_rallies) if teso_core==1
egen orkik=total(odinga_rallies) if kikuyu_core==1
egen orluo=total(odinga_rallies) if luo_core==1
egen orkam=total(odinga_rallies) if kamba_core==1

sort orl
replace orl=orl[_n-1] if orl==.
sort ork
replace ork=ork[_n-1] if ork==.
sort orki
replace orki=orki[_n-1] if orki==.
sort orme
replace orme=orme[_n-1] if orme==.
sort ormi
replace ormi=ormi[_n-1] if ormi==.
sort ormas
replace ormas= ormas[_n-1] if ormas==.
sort orturk
replace orturk= orturk[_n-1] if orturk==.
sort oremb
replace oremb= oremb[_n-1] if oremb==.
sort ortai
replace ortai= ortai[_n-1] if ortai==.
sort orteso
replace orteso= orteso[_n-1] if orteso==.
sort orkik
replace orkik= orkik[_n-1] if orkik==.
sort orluo
replace orluo= orluo[_n-1] if orluo==.
sort orkam
replace orkam= orkam[_n-1] if orkam==.

sort num
replace odishare=orl/79 if _n==1
replace odishare=ork/79 if _n==2
replace odishare=orki/79 if _n==3
replace odishare=orme/79 if _n==4
replace odishare=ormi/79 if _n==5
replace odishare =ormas/79 if _n==6
replace odishare =orturk/79 if _n==7
replace odishare =oremb/79 if _n==8
replace odishare =ortai/79 if _n==9
replace odishare =orteso/79 if _n==10
replace odishare =orkik/79 if _n==11
replace odishare =orluo/79 if _n==12
replace odishare =orkam/79 if _n==13

*musyoka
egen mrl=total(musyoka_rallies) if luhya_core==1
egen mrk=total(musyoka_rallies) if kalenjin_core==1
egen mrki=total(musyoka_rallies) if kisii_core==1
egen mrme=total(musyoka_rallies) if meru_core==1
egen mrmi=total(musyoka_rallies) if mijikenda_core==1
egen mrmas=total(musyoka_rallies) if maasai_core==1
egen mrturk=total(musyoka_rallies) if turkana_core==1
egen mremb=total(musyoka_rallies) if embu_core==1
egen mrtai=total(musyoka_rallies) if taita_core==1
egen mrteso=total(musyoka_rallies) if teso_core==1
egen mrkik=total(musyoka_rallies) if kikuyu_core==1
egen mrluo=total(musyoka_rallies) if luo_core==1
egen mrkam=total(musyoka_rallies) if kamba_core==1

sort mrl
replace mrl=mrl[_n-1] if mrl==.
sort mrk
replace mrk=mrk[_n-1] if mrk==.
sort mrki
replace mrki=mrki[_n-1] if mrki==.
sort mrme
replace mrme=mrme[_n-1] if mrme==.
sort mrmi
replace mrmi=mrmi[_n-1] if mrmi==.
sort mrmas
replace mrmas= mrmas[_n-1] if mrmas==.
sort mrturk
replace mrturk= mrturk[_n-1] if mrturk==.
sort mremb
replace mremb= mremb[_n-1] if mremb==.
sort mrtai
replace mrtai= mrtai[_n-1] if mrtai==.
sort mrteso
replace mrteso= mrteso[_n-1] if mrteso==.
sort mrkik
replace mrkik= mrkik[_n-1] if mrkik==.
sort mrluo
replace mrluo= mrluo[_n-1] if mrluo==.
sort mrkam
replace mrkam= mrkam[_n-1] if mrkam==.

sort num
replace musshare=mrl/70 if _n==1
replace musshare =mrk/70 if _n==2
replace musshare =mrki/70 if _n==3
replace musshare =mrme/70 if _n==4
replace musshare =mrmi/70 if _n==5
replace musshare =mrmas/70 if _n==6
replace musshare =mrturk/70 if _n==7
replace musshare =mremb/70 if _n==8
replace musshare =mrtai/70 if _n==9
replace musshare =mrteso/70 if _n==10
replace musshare =mrkik/70 if _n==11
replace musshare =mrluo/70 if _n==12
replace musshare =mrkam/70 if _n==13

*graph
graph bar kibshare odishare musshare if num<11, ///
over(num, relabel(1 `" "Luhya" "(9.9%)"' 2 `" "Kalenjin" "(9.6%)"' 3  `" "Kisii" "(4.8%)"'  4  `" "Meru" "(5.1%)"'  5  `" "Mijikenda" "(4.0%)"' ///
6 `" "Maasai" "(1.6%)"' 7 `" "Turkana" "(0.9%)"' 8  `" "Embu" "(1.1%)"'  9  `" "Taita" "(0.9%)"' 10  `" "Teso" "(0.6%)"')) ///
scheme(s1mono) ///
bar(1, col(black)) bar(2, col(gs7)) bar(3, fcol(gs12)) ///
ytitle("Percent of Rallies") ///
ylabel(0 "0" .05 "5%" .1 "10%" .15 "15%", angle(0) nogrid) ///
legend(rows(1) label(1 "Kibaki") label(2 "Odinga") label(3 "Musyoka")) plotregion(lcolor(white)) scale(1.22) aspectratio(.3) xsize(8) title("(a) 2007")


***2013 election
use "rally_data2013.dta", clear

gen kikuyu_core=0
replace kikuyu_core=1 if kikuyu_share>=.5
gen luo_core=0
replace luo_core=1 if luo_share>=.5
gen kamba_core=0
replace kamba_core=1 if kamba_share>=.5
gen luhya_core=0
replace luhya_core=1 if luhya_share>=.5
gen kalenjin_core=0
replace kalenjin_core=1 if kalenjin_share>=.5
gen kisii_core=0
replace kisii_core=1 if kisii_share>=.5
gen meru_core=0
replace meru_core=1 if meru_share>=.5
gen mijikenda_core=0
replace mijikenda_core=1 if mijikenda_share>=.5
gen maasai_core=0
replace maasai_core=1 if maasai_share>=.5
gen turkana_core=0
replace turkana_core=1 if turkana_share>=.5
gen embu_core=0
replace embu_core=1 if embu_share>=.5
gen taita_core=0
replace taita_core=1 if taita_share>=.5
gen teso_core=0
replace teso_core=1 if teso_share>=.5

*voter shares by ethnic areas
egen luhya_core_voters=total(voters) if luhya_core==1
egen kalenjin_core_voters=total(voters) if kalenjin_core==1
egen kisii_core_voters=total(voters) if kisii_core==1
egen meru_core_voters=total(voters) if meru_core==1
egen mijikenda_core_voters=total(voters) if mijikenda_core==1
egen maasai_core_voters=total(voters) if maasai_core==1
egen turkana_core_voters=total(voters) if turkana_core==1
egen embu_core_voters=total(voters) if embu_core==1
egen taita_core_voters=total(voters) if taita_core==1
egen teso_core_voters=total(voters) if teso_core==1
egen kamba_core_voters=total(voters) if kamba_core==1
egen total_voters=total(voters)

gen luhya_core_share= luhya_core_voters/total_voters
gen kalenjin_core_share= kalenjin_core_voters/total_voters
gen kisii_core_share= kisii_core_voters/total_voters
gen meru_core_share= meru_core_voters/total_voters
gen mijikenda_core_share= mijikenda_core_voters/total_voters
gen maasai_core_share= maasai_core_voters/total_voters
gen turkana_core_share= turkana_core_voters/total_voters
gen embu_core_share= embu_core_voters/total_voters
gen taita_core_share= taita_core_voters/total_voters
gen teso_core_share= teso_core_voters/total_voters
gen kamba_core_share= kamba_core_voters/total_voters

tab luhya_core_share 
tab kalenjin_core_share 
tab kisii_core_share 
tab meru_core_share 
tab mijikenda_core_share 
tab maasai_core_share 
tab turkana_core_share 
tab embu_core_share 
tab taita_core_share 
tab teso_core_share
tab kamba_core_share

gen group="" in 1/13
replace group="1 Luhya Area" if _n==1
replace group="2 Kalenjin Area" if _n==2
replace group="3 Kisii Area" if _n==3
replace group="4 Meru Area" if _n==4
replace group="5 Mijikenda Area" if _n==5
replace group="6 Maasai Area" if _n==6
replace group="7 Turkana Area" if _n==7
replace group="8 Embu Area" if _n==8
replace group="9 Taita Area" if _n==9
replace group="10 Teso Area" if _n==10
replace group="11 Kikuyu Area" if _n==11
replace group="12 Luo Area" if _n==12
replace group="13 Kamba Area" if _n==13

gen num=_n in 1/13
gen kenyatta_share=. in 1/13
gen odinga_share=. in 1/13

*kenyatta
egen kenr_luhya=total(kenyatta_rallies) if luhya_core==1
egen kenr_kalenjin=total(kenyatta_rallies) if kalenjin_core==1
egen kenr_kisii=total(kenyatta_rallies) if kisii_core==1
egen kenr_meru=total(kenyatta_rallies) if meru_core==1
egen kenr_mijikenda=total(kenyatta_rallies) if mijikenda_core==1
egen kenr_maasai=total(kenyatta_rallies) if maasai_core==1
egen kenr_turkana=total(kenyatta_rallies) if turkana_core==1
egen kenr_embu=total(kenyatta_rallies) if embu_core==1
egen kenr_taita=total(kenyatta_rallies) if taita_core==1
egen kenr_teso=total(kenyatta_rallies) if teso_core==1
egen kenr_kikuyu=total(kenyatta_rallies) if kikuyu_core==1
egen kenr_luo=total(kenyatta_rallies) if luo_core==1
egen kenr_kamba=total(kenyatta_rallies) if kamba_core==1

sort kenr_luhya
replace kenr_luhya = kenr_luhya[_n-1] if kenr_luhya ==.
sort kenr_kalenjin
replace kenr_kalenjin = kenr_kalenjin[_n-1] if kenr_kalenjin ==.
sort kenr_kisii
replace kenr_kisii = kenr_kisii[_n-1] if kenr_kisii ==.
sort kenr_meru
replace kenr_meru = kenr_meru[_n-1] if kenr_meru ==.
sort kenr_mijikenda
replace kenr_mijikenda = kenr_mijikenda[_n-1] if kenr_mijikenda ==.
sort kenr_maasai
replace kenr_maasai = kenr_maasai[_n-1] if kenr_maasai ==.
sort kenr_turkana
replace kenr_turkana = kenr_turkana[_n-1] if kenr_turkana ==.
sort kenr_embu
replace kenr_embu = kenr_embu[_n-1] if kenr_embu ==.
sort kenr_taita
replace kenr_taita = kenr_taita[_n-1] if kenr_taita ==.
sort kenr_teso
replace kenr_teso = kenr_teso[_n-1] if kenr_teso ==.
sort kenr_kikuyu
replace kenr_kikuyu = kenr_kikuyu[_n-1] if kenr_kikuyu ==.
sort kenr_luo
replace kenr_luo = kenr_luo[_n-1] if kenr_luo ==.
sort kenr_kamba
replace kenr_kamba = kenr_kamba[_n-1] if kenr_kamba ==.

egen kenyatta_rallies_total=total(kenyatta_rallies)

sort num
replace kenyatta_share = kenr_luhya / kenyatta_rallies_total if _n==1
replace kenyatta_share = kenr_kalenjin / kenyatta_rallies_total if _n==2
replace kenyatta_share = kenr_kisii / kenyatta_rallies_total if _n==3
replace kenyatta_share = kenr_meru / kenyatta_rallies_total if _n==4
replace kenyatta_share = kenr_mijikenda / kenyatta_rallies_total if _n==5
replace kenyatta_share = kenr_maasai / kenyatta_rallies_total if _n==6
replace kenyatta_share = kenr_turkana / kenyatta_rallies_total if _n==7
replace kenyatta_share = kenr_embu / kenyatta_rallies_total if _n==8
replace kenyatta_share = kenr_taita / kenyatta_rallies_total if _n==9
replace kenyatta_share = kenr_teso / kenyatta_rallies_total if _n==10
replace kenyatta_share = kenr_kikuyu / kenyatta_rallies_total if _n==11
replace kenyatta_share = kenr_luo / kenyatta_rallies_total if _n==12
replace kenyatta_share = kenr_kamba / kenyatta_rallies_total if _n==13

*odinga
egen odir_luhya=total(odinga_rallies) if luhya_core==1
egen odir_kalenjin=total(odinga_rallies) if kalenjin_core==1
egen odir_kisii=total(odinga_rallies) if kisii_core==1
egen odir_meru=total(odinga_rallies) if meru_core==1
egen odir_mijikenda=total(odinga_rallies) if mijikenda_core==1
egen odir_maasai=total(odinga_rallies) if maasai_core==1
egen odir_turkana=total(odinga_rallies) if turkana_core==1
egen odir_embu=total(odinga_rallies) if embu_core==1
egen odir_taita=total(odinga_rallies) if taita_core==1
egen odir_teso=total(odinga_rallies) if teso_core==1
egen odir_kikuyu=total(odinga_rallies) if kikuyu_core==1
egen odir_luo=total(odinga_rallies) if luo_core==1
egen odir_kamba=total(odinga_rallies) if kamba_core==1

sort odir_luhya
replace odir_luhya = odir_luhya[_n-1] if odir_luhya ==.
sort odir_kalenjin
replace odir_kalenjin = odir_kalenjin[_n-1] if odir_kalenjin ==.
sort odir_kisii
replace odir_kisii = odir_kisii[_n-1] if odir_kisii ==.
sort odir_meru
replace odir_meru = odir_meru[_n-1] if odir_meru ==.
sort odir_mijikenda
replace odir_mijikenda = odir_mijikenda[_n-1] if odir_mijikenda ==.
sort odir_maasai
replace odir_maasai = odir_maasai[_n-1] if odir_maasai ==.
sort odir_turkana
replace odir_turkana = odir_turkana[_n-1] if odir_turkana ==.
sort odir_embu
replace odir_embu = odir_embu[_n-1] if odir_embu ==.
sort odir_taita
replace odir_taita = odir_taita[_n-1] if odir_taita ==.
sort odir_teso
replace odir_teso = odir_teso[_n-1] if odir_teso ==.
sort odir_kikuyu
replace odir_kikuyu = odir_kikuyu[_n-1] if odir_kikuyu ==.
sort odir_luo
replace odir_luo = odir_luo[_n-1] if odir_luo ==.
sort odir_kamba
replace odir_kamba = odir_kamba[_n-1] if odir_kamba ==.

egen odinga_rallies_total=total(odinga_rallies)

sort num
replace odinga_share = odir_luhya / odinga_rallies_total if _n==1
replace odinga_share = odir_kalenjin / odinga_rallies_total if _n==2
replace odinga_share = odir_kisii / odinga_rallies_total if _n==3
replace odinga_share = odir_meru / odinga_rallies_total if _n==4
replace odinga_share = odir_mijikenda / odinga_rallies_total if _n==5
replace odinga_share = odir_maasai / odinga_rallies_total if _n==6
replace odinga_share = odir_turkana / odinga_rallies_total if _n==7
replace odinga_share = odir_embu / odinga_rallies_total if _n==8
replace odinga_share = odir_taita / odinga_rallies_total if _n==9
replace odinga_share = odir_teso / odinga_rallies_total if _n==10
replace odinga_share = odir_kikuyu / odinga_rallies_total if _n==11
replace odinga_share = odir_luo / odinga_rallies_total if _n==12
replace odinga_share = odir_kamba / odinga_rallies_total if _n==13

recode num (1=1) (2=2) (13=3) (3=4) (4=5) (5=6) (6=7) (7=8) (8=9) (9=10) (else=.), gen(num2)
sort num2

*graph
graph bar kenyatta_share odinga_share if num2<11, ///
over(num2, relabel(1 `" "Luhya" "(9.9%)"' 2 `" "Kalenjin" "(9.6%)"' 3  `" "Kamba" "(8.6%)"'  4  `" "Kisii" "(4.8%)"'  5  `" "Meru" "(5.1%)"' ///
6 `" "Mijikenda" "(4.0%)"' 7 `" "Maasai" "(1.6%)"' 8  `" "Turkana" "(0.9%)"' 9  `" "Embu" "(1.1%)"' 10  `" "Taita" "(0.9%)"') gap(*3.5) label(noticks) ) ///
scheme(s1mono) ///
bar(1, fcol(gs0)) bar(2, fcol(gs10)) ///
ytitle("Percent of Rallies") ///
legend(rows(1) label(1 "Kenyatta") label(2 "Odinga")) xsize(7) ///
plotregion(lcolor(white))  ylabel(0 "0" .05 "5%" .1 "10%" .15 "15%" .2 "20%", angle(0) nogrid)   scale(1.1) aspectratio(.3) title("(b) 2013")
 

***2017 election
use "rally_data2017.dta", clear

gen kikuyu_core=0
replace kikuyu_core=1 if kikuyu_share>=.5
gen luo_core=0
replace luo_core=1 if luo_share>=.5
gen kamba_core=0
replace kamba_core=1 if kamba_share>=.5
gen luhya_core=0
replace luhya_core=1 if luhya_share>=.5
gen kalenjin_core=0
replace kalenjin_core=1 if kalenjin_share>=.5
gen kisii_core=0
replace kisii_core=1 if kisii_share>=.5
gen meru_core=0
replace meru_core=1 if meru_share>=.5
gen mijikenda_core=0
replace mijikenda_core=1 if mijikenda_share>=.5
gen maasai_core=0
replace maasai_core=1 if maasai_share>=.5
gen turkana_core=0
replace turkana_core=1 if turkana_share>=.5
gen embu_core=0
replace embu_core=1 if embu_share>=.5
gen taita_core=0
replace taita_core=1 if taita_share>=.5
gen teso_core=0
replace teso_core=1 if teso_share>=.5

*voter shares by ethnic areas
egen luhya_core_voters=total(voters) if luhya_core==1
egen kalenjin_core_voters=total(voters) if kalenjin_core==1
egen kisii_core_voters=total(voters) if kisii_core==1
egen meru_core_voters=total(voters) if meru_core==1
egen mijikenda_core_voters=total(voters) if mijikenda_core==1
egen maasai_core_voters=total(voters) if maasai_core==1
egen turkana_core_voters=total(voters) if turkana_core==1
egen embu_core_voters=total(voters) if embu_core==1
egen taita_core_voters=total(voters) if taita_core==1
egen teso_core_voters=total(voters) if teso_core==1
egen kamba_core_voters=total(voters) if kamba_core==1
egen total_voters=total(voters)

gen luhya_core_share= luhya_core_voters/total_voters
gen kalenjin_core_share= kalenjin_core_voters/total_voters
gen kisii_core_share= kisii_core_voters/total_voters
gen meru_core_share= meru_core_voters/total_voters
gen mijikenda_core_share= mijikenda_core_voters/total_voters
gen maasai_core_share= maasai_core_voters/total_voters
gen turkana_core_share= turkana_core_voters/total_voters
gen embu_core_share= embu_core_voters/total_voters
gen taita_core_share= taita_core_voters/total_voters
gen teso_core_share= teso_core_voters/total_voters
gen kamba_core_share= kamba_core_voters/total_voters

tab luhya_core_share 
tab kalenjin_core_share 
tab kisii_core_share 
tab meru_core_share 
tab mijikenda_core_share 
tab maasai_core_share 
tab turkana_core_share 
tab embu_core_share 
tab taita_core_share 
tab teso_core_share
tab kamba_core_share

gen group="" in 1/13
replace group="1 Luhya Area" if _n==1
replace group="2 Kalenjin Area" if _n==2
replace group="3 Kisii Area" if _n==3
replace group="4 Meru Area" if _n==4
replace group="5 Mijikenda Area" if _n==5
replace group="6 Maasai Area" if _n==6
replace group="7 Turkana Area" if _n==7
replace group="8 Embu Area" if _n==8
replace group="9 Taita Area" if _n==9
replace group="10 Teso Area" if _n==10
replace group="11 Kikuyu Area" if _n==11
replace group="12 Luo Area" if _n==12
replace group="13 Kamba Area" if _n==13

gen num=_n in 1/13
gen kenyatta_share=. in 1/13
gen odinga_share=. in 1/13

*kenyatta
egen kenr_luhya=total(kenyatta_rallies) if luhya_core==1
egen kenr_kalenjin=total(kenyatta_rallies) if kalenjin_core==1
egen kenr_kisii=total(kenyatta_rallies) if kisii_core==1
egen kenr_meru=total(kenyatta_rallies) if meru_core==1
egen kenr_mijikenda=total(kenyatta_rallies) if mijikenda_core==1
egen kenr_maasai=total(kenyatta_rallies) if maasai_core==1
egen kenr_turkana=total(kenyatta_rallies) if turkana_core==1
egen kenr_embu=total(kenyatta_rallies) if embu_core==1
egen kenr_taita=total(kenyatta_rallies) if taita_core==1
egen kenr_teso=total(kenyatta_rallies) if teso_core==1
egen kenr_kikuyu=total(kenyatta_rallies) if kikuyu_core==1
egen kenr_luo=total(kenyatta_rallies) if luo_core==1
egen kenr_kamba=total(kenyatta_rallies) if kamba_core==1

sort kenr_luhya
replace kenr_luhya = kenr_luhya[_n-1] if kenr_luhya ==.
sort kenr_kalenjin
replace kenr_kalenjin = kenr_kalenjin[_n-1] if kenr_kalenjin ==.
sort kenr_kisii
replace kenr_kisii = kenr_kisii[_n-1] if kenr_kisii ==.
sort kenr_meru
replace kenr_meru = kenr_meru[_n-1] if kenr_meru ==.
sort kenr_mijikenda
replace kenr_mijikenda = kenr_mijikenda[_n-1] if kenr_mijikenda ==.
sort kenr_maasai
replace kenr_maasai = kenr_maasai[_n-1] if kenr_maasai ==.
sort kenr_turkana
replace kenr_turkana = kenr_turkana[_n-1] if kenr_turkana ==.
sort kenr_embu
replace kenr_embu = kenr_embu[_n-1] if kenr_embu ==.
sort kenr_taita
replace kenr_taita = kenr_taita[_n-1] if kenr_taita ==.
sort kenr_teso
replace kenr_teso = kenr_teso[_n-1] if kenr_teso ==.
sort kenr_kikuyu
replace kenr_kikuyu = kenr_kikuyu[_n-1] if kenr_kikuyu ==.
sort kenr_luo
replace kenr_luo = kenr_luo[_n-1] if kenr_luo ==.
sort kenr_kamba
replace kenr_kamba = kenr_kamba[_n-1] if kenr_kamba ==.

egen kenyatta_rallies_total=total(kenyatta_rallies)

sort num
replace kenyatta_share = kenr_luhya / kenyatta_rallies_total if _n==1
replace kenyatta_share = kenr_kalenjin / kenyatta_rallies_total if _n==2
replace kenyatta_share = kenr_kisii / kenyatta_rallies_total if _n==3
replace kenyatta_share = kenr_meru / kenyatta_rallies_total if _n==4
replace kenyatta_share = kenr_mijikenda / kenyatta_rallies_total if _n==5
replace kenyatta_share = kenr_maasai / kenyatta_rallies_total if _n==6
replace kenyatta_share = kenr_turkana / kenyatta_rallies_total if _n==7
replace kenyatta_share = kenr_embu / kenyatta_rallies_total if _n==8
replace kenyatta_share = kenr_taita / kenyatta_rallies_total if _n==9
replace kenyatta_share = kenr_teso / kenyatta_rallies_total if _n==10
replace kenyatta_share = kenr_kikuyu / kenyatta_rallies_total if _n==11
replace kenyatta_share = kenr_luo / kenyatta_rallies_total if _n==12
replace kenyatta_share = kenr_kamba / kenyatta_rallies_total if _n==13

*odinga
egen odir_luhya=total(odinga_rallies) if luhya_core==1
egen odir_kalenjin=total(odinga_rallies) if kalenjin_core==1
egen odir_kisii=total(odinga_rallies) if kisii_core==1
egen odir_meru=total(odinga_rallies) if meru_core==1
egen odir_mijikenda=total(odinga_rallies) if mijikenda_core==1
egen odir_maasai=total(odinga_rallies) if maasai_core==1
egen odir_turkana=total(odinga_rallies) if turkana_core==1
egen odir_embu=total(odinga_rallies) if embu_core==1
egen odir_taita=total(odinga_rallies) if taita_core==1
egen odir_teso=total(odinga_rallies) if teso_core==1
egen odir_kikuyu=total(odinga_rallies) if kikuyu_core==1
egen odir_luo=total(odinga_rallies) if luo_core==1
egen odir_kamba=total(odinga_rallies) if kamba_core==1

sort odir_luhya
replace odir_luhya = odir_luhya[_n-1] if odir_luhya ==.
sort odir_kalenjin
replace odir_kalenjin = odir_kalenjin[_n-1] if odir_kalenjin ==.
sort odir_kisii
replace odir_kisii = odir_kisii[_n-1] if odir_kisii ==.
sort odir_meru
replace odir_meru = odir_meru[_n-1] if odir_meru ==.
sort odir_mijikenda
replace odir_mijikenda = odir_mijikenda[_n-1] if odir_mijikenda ==.
sort odir_maasai
replace odir_maasai = odir_maasai[_n-1] if odir_maasai ==.
sort odir_turkana
replace odir_turkana = odir_turkana[_n-1] if odir_turkana ==.
sort odir_embu
replace odir_embu = odir_embu[_n-1] if odir_embu ==.
sort odir_taita
replace odir_taita = odir_taita[_n-1] if odir_taita ==.
sort odir_teso
replace odir_teso = odir_teso[_n-1] if odir_teso ==.
sort odir_kikuyu
replace odir_kikuyu = odir_kikuyu[_n-1] if odir_kikuyu ==.
sort odir_luo
replace odir_luo = odir_luo[_n-1] if odir_luo ==.
sort odir_kamba
replace odir_kamba = odir_kamba[_n-1] if odir_kamba ==.

egen odinga_rallies_total=total(odinga_rallies)

sort num
replace odinga_share = odir_luhya / odinga_rallies_total if _n==1
replace odinga_share = odir_kalenjin / odinga_rallies_total if _n==2
replace odinga_share = odir_kisii / odinga_rallies_total if _n==3
replace odinga_share = odir_meru / odinga_rallies_total if _n==4
replace odinga_share = odir_mijikenda / odinga_rallies_total if _n==5
replace odinga_share = odir_maasai / odinga_rallies_total if _n==6
replace odinga_share = odir_turkana / odinga_rallies_total if _n==7
replace odinga_share = odir_embu / odinga_rallies_total if _n==8
replace odinga_share = odir_taita / odinga_rallies_total if _n==9
replace odinga_share = odir_teso / odinga_rallies_total if _n==10
replace odinga_share = odir_kikuyu / odinga_rallies_total if _n==11
replace odinga_share = odir_luo / odinga_rallies_total if _n==12
replace odinga_share = odir_kamba / odinga_rallies_total if _n==13

recode num (1=1) (2=2) (13=3) (3=4) (4=5) (5=6) (6=7) (7=8) (8=9) (9=10) (else=.), gen(num2)
sort num2

*graph
graph bar kenyatta_share odinga_share if num2<11, ///
over(num2, relabel(1 `" "Luhya" "(9.9%)"' 2 `" "Kalenjin" "(9.6%)"' 3  `" "Kamba" "(8.6%)"'  4  `" "Kisii" "(4.8%)"'  5  `" "Meru" "(5.1%)"' ///
6 `" "Mijikenda" "(4.0%)"' 7 `" "Maasai" "(1.6%)"' 8  `" "Turkana" "(0.9%)"' 9  `" "Embu" "(1.1%)"' 10  `" "Taita" "(0.9%)"') gap(*3.5) label(noticks) ) ///
scheme(s1mono) ///
bar(1, fcol(gs0)) bar(2, fcol(gs10)) ///
ytitle("Percent of Rallies") ///
legend(rows(1) label(1 "Kenyatta") label(2 "Odinga")) xsize(7) /// 
plotregion(lcolor(white))  ylabel(0 "0" .05 "5%" .1 "10%" .15 "15%" .2 "20%", angle(0) nogrid)  scale(1.1) aspectratio(.3) title("(c) 2017")
 



**************************************************************
********** Figure 4.2 ****************************************
**************************************************************


use "contact_data.dta", clear

***MAIN SPECIFICATION
set more off
matrix res = (1,2,3\4,5,6\7,8,9)
matrix coln res = me ll95 ul95
matrix rown res = PNU ODM ODM-K

matrix res2 = (1,2,3\4,5,6)
matrix coln res2 = me ll95 ul95
matrix rown res2 = NCO CO

*PNU
logit pnu_con kikuyu voters area density margin groups leader female age ed reg [pweight=weight], cluster(connum)
est store model
estout model using "/Users/jeremyhorowitz/Documents/Kenya/Ch 3 - Campaign Strategy/model2", ///
stats(N r2_p, labels("N" "Pseudo R-squared")) cells(b(star fmt (%9.3f)) p(par)) starlevels(+ 0.10 * 0.05 ** 0.01) label unstack replace
margins, dydx(kikuyu) at((mean) _all (median) leader female reg) post level(90)
mat b = r(table)
matrix res[1,1] = b[1,1]
matrix res[1,2] = b[5,1]
matrix res[1,3] = b[6,1]

logit pnu_con kikuyu voters area density margin groups leader female age ed reg [pweight=weight], cluster(connum)
margins, at(kikuyu=(0 1) (mean) _all (median) leader female reg) post level(90)
mat c = r(table)
matrix res2[1,1] = c[1,2]
matrix res2[1,2] = c[5,2]
matrix res2[1,3] = c[6,2]
matrix res2[2,1] = c[1,1]
matrix res2[2,2] = c[5,1]
matrix res2[2,3] = c[6,1]

coefplot matrix(res2[.,1]), ci((res2[.,2] res2[.,3])) ///
ytitle("") legend(off) scheme(s1mono) vertical ///
aspectratio(1) ylabel(0(.25).75, angle(0)) plotregion(lcolor(white)) scale(1.2) mlabel format(%9.2f) rename(NCO = "Kikuyu area" CO = "Other") title(PNU) name(a, replace)

*ODM
set more off
logit odm_con luo voters area density margin groups leader female age ed reg [pweight=weight], cluster(connum)
est store model
estout model using "/Users/jeremyhorowitz/Documents/Kenya/Ch 3 - Campaign Strategy/model2", ///
stats(N r2_p, labels("N" "Pseudo R-squared")) cells(b(star fmt (%9.3f)) p(par)) starlevels(+ 0.10 * 0.05 ** 0.01) label unstack append
margins, dydx(luo) at((mean) _all (median) leader female reg) post level(90)
mat b = r(table)
matrix res[2,1] = b[1,1]
matrix res[2,2] = b[5,1]
matrix res[2,3] = b[6,1]

logit odm_con luo voters area density margin groups leader female age ed reg [pweight=weight], cluster(connum)
margins, at(luo=(0 1) (mean) _all (median) leader female reg) post level(90)
mat c = r(table)
matrix res2[1,1] = c[1,2]
matrix res2[1,2] = c[5,2]
matrix res2[1,3] = c[6,2]
matrix res2[2,1] = c[1,1]
matrix res2[2,2] = c[5,1]
matrix res2[2,3] = c[6,1]

coefplot matrix(res2[.,1]), ci((res2[.,2] res2[.,3])) ///
ytitle("") legend(off) scheme(s1mono) vertical ///
aspectratio(1) ylabel(0(.25).75, angle(0)) plotregion(lcolor(white)) scale(1.2) mlabel format(%9.2f) rename(NCO = "Luo area" CO = "Other") title(ODM) name(b, replace)

*ODM-K
set more off
logit odmk_con kamba voters area density margin groups leader female age ed reg [pweight=weight], cluster(connum)
est store model
estout model using "/Users/jeremyhorowitz/Documents/Kenya/Ch 3 - Campaign Strategy/model2", ///
stats(N r2_p, labels("N" "Pseudo R-squared")) cells(b(star fmt (%9.3f)) p(par)) starlevels(+ 0.10 * 0.05 ** 0.01) label unstack append
margins, dydx(kamba) at((mean) _all (median) leader female reg) post level(90)
mat b = r(table)
matrix res[3,1] = b[1,1]
matrix res[3,2] = b[5,1]
matrix res[3,3] = b[6,1]

logit odmk_con kamba voters area density margin groups leader female age ed reg [pweight=weight], cluster(connum)
margins, at(kamba=(0 1) (mean) _all (median) leader female reg) post level(90)
mat c = r(table)
matrix res2[1,1] = c[1,2]
matrix res2[1,2] = c[5,2]
matrix res2[1,3] = c[6,2]
matrix res2[2,1] = c[1,1]
matrix res2[2,2] = c[5,1]
matrix res2[2,3] = c[6,1]

coefplot matrix(res2[.,1]), ci((res2[.,2] res2[.,3])) ///
ytitle("") legend(off) scheme(s1mono) vertical ///
aspectratio(1) ylabel(0(.25).75, angle(0)) plotregion(lcolor(white)) scale(1.2) mlabel format(%9.2f) rename(NCO = "Kamba area" CO = "Other") title(ODM-K) name(c, replace)

graph combine a b c, row(1) ycommon scheme(s1mono) xsize(8) iscale(1.025) xcommon l1title("Pr(household contact)")

































